home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Development / Source / GENetReleaseƒ / GEBreakout / BGame.c next >
Text File  |  1994-03-07  |  6KB  |  248 lines

  1. /*
  2.     BGame.c
  3.     
  4.     Game running code for Breakout Demo
  5.     
  6.     Copyright 1994 by Al Evans. All rights reserved.
  7.     
  8.     2/24/94
  9.     
  10. */
  11.  
  12. #include "BGame.h"
  13. #include "Motion.h"
  14.  
  15. short    gBOBrickCount;
  16. short    gBOBallCount;
  17. short    gBOBallSpeed;
  18. long    gBOScore;
  19. Str255    gBOScoreStr;
  20.  
  21. GrafElPtr MakeABrick(GEWorldPtr world, OSType id)
  22. {
  23.     GrafElPtr    aBrick;
  24.     
  25.     aBrick = FindElementByID(world, id);
  26.     if (aBrick == nil) {
  27.         aBrick = NewBasicPICT(world, id, brickPlane, rBrick, transparent, 0, 0);
  28.     }
  29.     return aBrick;
  30. }
  31.  
  32. //Setup
  33. Boolean LoadBreakoutGame(GEWorldPtr world)
  34. {
  35.     GrafElPtr    thisElement;
  36.     MParamPtr     ballMotion;
  37.     RGBColor     scoreColor;
  38.     short        scoreFNum;
  39.     
  40.     //Get background
  41.     thisElement = NewTiledGraphic(world, bkgID, bkgPlane, rBkg, srcCopy, world->animationRect);
  42.     if (thisElement == nil) return false;
  43.     
  44.     //Get paddle
  45.     thisElement = NewBasicPICT(world, paddleID, paddlePlane, rPaddle, transparent, 0, world->animationRect.bottom - 16);
  46.     if (thisElement == nil) return false;
  47.     //Set paddle's autochange proc
  48.     SetAutoChange(world, paddleID, DoPaddle, nil, 33);
  49.     
  50.     //Get ball
  51.     thisElement = NewBasicPICT(world, ballID, ballPlane, rBall, transparent, 0, 0);
  52.     if (thisElement == nil) return false;
  53.     //Hide it for now
  54.     ShowElement(world, ballID, false);
  55.     //Set ball's autochange and collision procs
  56.     ballMotion = (MParamPtr) NewPtrClear(sizeof(MotionParams));
  57.     InitMotion(ballMotion, 0, 125);
  58.     ballMotion->limitRect = world->animationRect;
  59.     SetAutoChange(world, ballID, DoBall, (Ptr) ballMotion, 17);
  60.     SetCollision(world, ballID, DoBallHit, brickPlane);  //brickPlane == paddlePlane
  61.     
  62.     //Get a brick just to make sure it's available
  63.     thisElement = MakeABrick(world, firstBrickID);
  64.     if (thisElement == nil) return false;
  65.     //Hide it
  66.     ShowElement(world, firstBrickID, false);
  67.     
  68.     //Make the scoreboard
  69.     scoreColor.red = 240 << 8;
  70.     scoreColor.green = 240 << 8;
  71.     scoreColor.blue = 46 << 8;
  72.     GetFNum("\pChicago", &scoreFNum);
  73.     thisElement = NewTextGraphic(world, scoreID, scorePlane, 20, 10, srcOr, 
  74.                         scoreFNum, bold, 12, scoreColor, gBOScoreStr);
  75.     if (thisElement == nil) return false;
  76.     SetAutoChange(world, scoreID, DoScore, nil, 200);  //Update score 5 times/second
  77.     
  78.     return true;
  79. }
  80.  
  81. void MakeNewBricks(GEWorldPtr world)
  82. {
  83.     GrafElPtr    thisBrick;
  84.     OSType        currBrickID = firstBrickID;
  85.     short        rowCount, colCount;
  86.     
  87.     gBOBrickCount = 0;
  88.     for (rowCount = 0; rowCount < 5; rowCount++) 
  89.         for (colCount = 0; colCount < 10; colCount++) {
  90.         thisBrick = MakeABrick(world, currBrickID);
  91.         if (thisBrick == nil) return;                 //Oops
  92.         MoveElementTo(world, currBrickID, 2 + colCount * 34, 36 + rowCount * 18);
  93.         ShowElement(world, currBrickID, true);
  94.         currBrickID++;
  95.         gBOBrickCount++;
  96.     }
  97. }
  98.  
  99. void StartNewBall(GEWorldPtr world)
  100. {
  101.     GrafElPtr    ball;
  102.     MParamPtr    ballMotion;
  103.     
  104.     ball = FindElementByID(world, ballID);
  105.     if (ball == nil) return;                        //Oops again
  106.     MoveElementTo(world, ballID, 0, 200);
  107.     ShowElement(world, ballID, true);
  108.     ballMotion = (MParamPtr) ball->changeData;
  109.     ballMotion->currMotion.v = gBOBallSpeed;
  110.     ballMotion->currMotion.h = gBOBallSpeed - 2;
  111. }
  112.  
  113. void NewBreakoutGame(GEWorldPtr world)
  114. {
  115.     
  116.     gBOScore = 0;
  117.     gBOScoreStr[0] = 0;
  118.     gBOBallCount = 4;
  119.     gBOBallSpeed = 4;
  120.     MakeNewBricks(world);
  121.     StartNewBall(world);
  122. }
  123.  
  124. //Return position of mouse in GEWorld coordinates
  125. short    GetPlayerMove(GEWorldPtr world)
  126. {
  127.     GrafPtr        savePort;
  128.     Point        saveFocus;
  129.     Point        mousePt;
  130.     short        move;
  131.  
  132.  
  133.     GetPort(&savePort);
  134.     SetPort((GrafPtr) world->gEWWindow);
  135.     GetGEWorldFocus(world, &saveFocus);
  136.     FocusOnGEWorld(world);
  137.  
  138.     GetMouse(&mousePt);
  139.     move = mousePt.h;
  140.     SetGEWorldFocus(world, saveFocus);
  141.     SetPort(savePort);
  142.     return move;
  143. }
  144.  
  145. pascal void DoPaddle(GEWorldPtr world, GrafElPtr paddle)
  146. {
  147.     short paddleX;
  148.     short limit = world->animationRect.right - (paddle->animationRect.right - paddle->animationRect.left);
  149.     
  150.     paddleX = GetPlayerMove(world);
  151.     if (paddleX < 0) paddleX = 0;
  152.     if (paddleX > limit) paddleX = limit;
  153.     PtrMoveElementTo(world, paddle, paddleX, paddle->animationRect.top);
  154. }
  155.  
  156.  
  157. pascal void DoBall(GEWorldPtr world, GrafElPtr ball)
  158. {
  159.     MParamPtr    motion = (MParamPtr) ball->changeData;
  160.     
  161.         switch (CheckLimits(&ball->animationRect, &motion->limitRect)) {
  162.             case up:
  163.                 if (motion->currMotion.v < 0)
  164.                     motion->currMotion.v = -motion->currMotion.v;
  165.                 break;
  166.             case left:
  167.                 if (motion->currMotion.h < 0)
  168.                     motion->currMotion.h = -motion->currMotion.h;
  169.                 break;
  170.             case down:
  171.                 ShowElement(world, ball->objectID, false);
  172.                 //Do something gamey here
  173.                 if (--gBOBallCount > 0)
  174.                     StartNewBall(world);
  175.                 break;
  176.             case right:
  177.                 if (motion->currMotion.h > 0)
  178.                     motion->currMotion.h = -motion->currMotion.h;
  179.                 break;
  180.         }
  181.         
  182.         PtrMoveElement(world, ball, motion->currMotion.h, motion->currMotion.v);
  183.  
  184. }
  185.  
  186. pascal void DoBallHit(GEWorldPtr world, GrafElPtr ball, GEDirection dir, GrafElPtr objHit)
  187. {
  188.     MParamPtr    motion = (MParamPtr) ball->changeData;
  189.     short         hBump;
  190.     
  191.     if (objHit->objectID == paddleID) {            //Ball hit the paddle
  192.         hBump = ball->animationRect.left - objHit->animationRect.left + 6;
  193.         hBump = hBump - 20;
  194.         hBump = hBump / 4;
  195.         motion->currMotion.h += hBump;
  196.     }
  197.     else {                                         //Ball hit a brick
  198.         ShowElement(world, objHit->objectID, false);
  199.         gBOScore += 50;
  200.         --gBOBrickCount;
  201.         if (gBOBrickCount == 0) {
  202.             gBOBallSpeed++;
  203.             MakeNewBricks(world);
  204.             StartNewBall(world);
  205.         }
  206.     }    
  207.     //Either way, bounce ball
  208.     switch (dir) {
  209.         case left:
  210.         case upLeft:
  211.         case downLeft:
  212.             if (motion->currMotion.h < 0)
  213.                 motion->currMotion.h = -motion->currMotion.h;
  214.             if (dir != left)
  215.                 motion->currMotion.v = -motion->currMotion.v;
  216.             break;
  217.         case right:
  218.         case upRight:
  219.         case downRight:
  220.             if (motion->currMotion.h > 0)
  221.                 motion->currMotion.h = -motion->currMotion.h;
  222.             if (dir != right)
  223.                 motion->currMotion.v = -motion->currMotion.v;
  224.             break;
  225.         case up:
  226.             if (motion->currMotion.v < 0)
  227.                 motion->currMotion.v = -motion->currMotion.v;
  228.             break;
  229.         case down:
  230.             if (motion->currMotion.v > 0)
  231.                 motion->currMotion.v = -motion->currMotion.v;
  232.             break;
  233.     }
  234.             
  235.  
  236. }
  237.  
  238. pascal void DoScore(GEWorldPtr world, GrafElPtr scoreBoard)
  239. {
  240.     static long scoreShown = 0;
  241.     
  242.     if (gBOScore != scoreShown) {
  243.         scoreShown = gBOScore;
  244.         NumToString(scoreShown, gBOScoreStr);
  245.         SetTextGraphicText(world, scoreBoard->objectID, gBOScoreStr);
  246.     }
  247. }
  248.